/*
 * Copyright 2019 Zhenjie Yan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.mingmen.canting.R;
import com.mingmen.canting.chushizhang.ChuShiRightAdapter;
import com.mingmen.canting.definerecycleview.ExpandableAdapter;
import com.mingmen.canting.jingli.JingLiCaiItemAdapter;
import com.mingmen.canting.model.Group;
import com.mingmen.canting.model.GroupMember;
import com.mingmen.canting.model.JingliCaiGouDanModel;
import com.mingmen.canting.model.ShopListModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zhenjie Yan on 1/30/19.
 */
public class RightExpandedAdapter extends ExpandableAdapter<ExpandableAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private List<JingliCaiGouDanModel.DataBean> mGroupList;
    Context context;
    private OnItemClickListener onitemClick;   //定义点击事件接口
    List<JingliCaiGouDanModel.DataBean.ApplyListBean> myLiveList;

    public interface OnItemClickListener {
        void onItemClickListener(int pos, List<JingliCaiGouDanModel.DataBean> mGroupList, List<JingliCaiGouDanModel.DataBean.ApplyListBean> myLiveList);
    }

    public void setOnitemClickLintener(OnItemClickListener onitemClick) {
        this.onitemClick = onitemClick;
    }

    public RightExpandedAdapter(Context context, List<JingliCaiGouDanModel.DataBean> mGroupList, List<JingliCaiGouDanModel.DataBean.ApplyListBean> myLiveList) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context;
        this.mGroupList = mGroupList;
        this.myLiveList = myLiveList;
    }

    public void setGroupList(List<JingliCaiGouDanModel.DataBean> groupList, List<JingliCaiGouDanModel.DataBean.ApplyListBean> myLiveList) {
        this.mGroupList = groupList;
        this.myLiveList = myLiveList;
        notifyDataSetChanged();

    }

    @Override
    public int parentItemCount() {
        return mGroupList == null ? 0 : mGroupList.size();
    }

    @Override
    public int childItemCount(int parentPosition) {
//        JingliCaiGouDanModel.DataBean
//        List<GroupMember> memberList = mGroupList.get(parentPosition).getMemberList();
        List<JingliCaiGouDanModel.DataBean.ApplyListBean> memberList = mGroupList.get(parentPosition).getApplyList();
        return memberList == null ? 0 : memberList.size();
    }

    @Override
    public ViewHolder createParentHolder(@NonNull ViewGroup root, int viewType) {
        View view = mInflater.inflate(R.layout.item_expand_parent, root, false);
        return new ParentHolder(view, this);
    }

    @Override
    public ViewHolder createChildHolder(@NonNull ViewGroup root, int viewType) {
        View view = mInflater.inflate(R.layout.item_expand_child, root, false);
        return new ChildHolder(view, this);
    }

    @Override
    public void bindParentHolder(@NonNull ViewHolder holder, int position) {
//        ((ParentHolder) holder).setData(mGroupList.get(position));
        ((ParentHolder) holder).setData(mGroupList.get(position));
        ((ParentHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onitemClick != null) {
//                    holder1.chuShiRightAdapter.setCheckedPosition(position);
                    onitemClick.onItemClickListener(position, mGroupList, myLiveList);
                }
            }
        });

    }

    @Override
    public void bindChildHolder(@NonNull ViewHolder holder, int parentPosition, int position) {
//        ((ChildHolder)holder).setData(mGroupList.get(parentPosition).getMemberList().get(position));
        ChildHolder holder1 = (ChildHolder) holder;
//        holder1.setData(mGroupList.get(parentPosition).getMemberList().get(position));
//        holder1.setData(mGroupList.get(parentPosition).getMemberList().get(position));
        List<String> second = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            second.add("Item_ITem" + i);
        }
//        holder.list.addAll(myLiveList.get(position));
        if (holder1.chuShiRightAdapter == null) {

            List<JingliCaiGouDanModel.DataBean.ApplyListBean> mgrouplist = mGroupList.get(position).getApplyList();

            holder1.chuShiRightAdapter = new JingLiCaiItemAdapter(context, mgrouplist, position);
            LinearLayoutManager layoutManage = new LinearLayoutManager(context);
            holder1.rv_item.setLayoutManager(layoutManage);
//            holder.rvItemItem.addItemDecoration(new GridSpacingItemDecoration(2, 20, false));
            holder1.rv_item.setAdapter(holder1.chuShiRightAdapter);
        } else {
            holder1.chuShiRightAdapter.setPosition(position);
            holder1.chuShiRightAdapter.notifyDataSetChanged();
        }

        holder1.chuShiRightAdapter.setOnitemClickLintener(new JingLiCaiItemAdapter.OnitemClick() {
            @Override
            public void onItemClick(int position, List<JingliCaiGouDanModel.DataBean.ApplyListBean> mList) {
                if (onitemClick != null) {
                    holder1.chuShiRightAdapter.setCheckedPosition(position);
                    onitemClick.onItemClickListener(position, mGroupList, mList);
                }
            }
        });


    }

    static class ParentHolder extends ViewHolder {

        TextView mTvTitle;
        ImageView mIvStatus;

        public ParentHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            mIvStatus = itemView.findViewById(R.id.iv_status);
        }

        /*  public void setData(Group data) {
              mTvTitle.setText(data.getName());
              mIvStatus.setSelected(data.isExpanded());
          }*/
        public void setData(JingliCaiGouDanModel.DataBean data) {
            mTvTitle.setText(data.getLimitData());
            mIvStatus.setSelected(data.isExpanded());

        }
    }

    static class ChildHolder extends ViewHolder {

        TextView mTvTitle;
        JingLiCaiItemAdapter chuShiRightAdapter;
        RecyclerView rv_item;

        public ChildHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            rv_item = itemView.findViewById(R.id.rv_item);
        }

        /* public void setData(GroupMember data) {
             mTvTitle.setText(data.getName());
         }*/
        public void setData(JingliCaiGouDanModel.DataBean data) {
            mTvTitle.setText(data.getLimitData());
        }
    }
}